home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gnulib / libsrc98.zoo / scanf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-29  |  11.8 KB  |  559 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.    This file is part of the GNU C Library.
  3.    
  4.    The GNU C Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.    
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU C Library; see the file COPYING.LIB.  If
  16.    not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17.    Cambridge, MA 02139, USA.  */
  18.  
  19. /* 
  20.  * adapted for atariST gcc lib
  21.  *
  22.  * NOTES: interface is different from equivalent gnuC lib function
  23.  *        it was munged to match our old _scanf().
  24.  *
  25.  *      if __NO_FLOAT__ is defined then the floating point stuff
  26.  *      gets nuked (for iio*.olb) as per our old scanf.c.
  27.  *
  28.  *  It is very important to read and understand the GNU Library General 
  29.  *  Public License. It specifies rights and conditions that are different
  30.  *  from the GNU copyleft.
  31.  *
  32.  *    ++jrb bammi@cadence.com
  33.  */
  34.  
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <ctype.h>
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <compiler.h>
  43.  
  44. /* the code assumes this definition, note: traditional <ctype> def
  45.  * of tolower will break the code. Ansi def should be ok.
  46.  */
  47. #ifdef tolower
  48. #  undef tolower
  49. #  define    tolower(c)    (isupper(c) ? (c)^0x20 : (c))
  50. #endif
  51.  
  52. #ifndef __NO_FLOAT__
  53. #  define FLOATS 1
  54. #else
  55. #  define FLOATS 0
  56. #endif
  57.  
  58. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  59.  
  60. #ifdef    __GNUC__
  61. #define    HAVE_LONGLONG
  62. #define    LONGLONG    long long
  63. #else
  64. #define    LONGLONG    long
  65. #endif
  66.  
  67.  
  68. #define    inchar()    ((c = ((*get)(s))) == EOF ? EOF : (++read_in, c))
  69. #define unchar(c)    (--read_in, (*unget)(c, s))
  70. #define    conv_error()    return((c == EOF || ((*unget)(c, s))), done)
  71. #define input_error()    return( done < 1 ? EOF : done )
  72. #define    memory_error()    return((errno = ENOMEM), EOF)
  73.  
  74. __EXTERN long int strtol __PROTO((const char *nptr, char **endptr, int base));
  75. __EXTERN double strtod __PROTO((const char *s, char **endptr));
  76.  
  77. /* Read formatted input from S according to the format string
  78.    FORMAT, using the argument list in ARG.
  79.    Return the number of assignments made, or -1 for an input error.  */
  80. int _scanf(s, get, unget, format, arg)
  81. register FILE *s;
  82. int (*get) __PROTO((FILE *));
  83. int (*unget) __PROTO((int, FILE *));
  84. const char *format;
  85. va_list arg;
  86. {
  87.     register const char *f = format;
  88.     register char fc;        /* Current character of the format.  */
  89.     register size_t done = 0;    /* Assignments done.  */
  90.     register size_t read_in = 0;    /* Chars read in.  */
  91.     register int c;        /* Last char read.  */
  92.     register int do_assign;    /* Whether to do an assignment.  */
  93.     register int width;        /* Maximum field width.  */
  94.     
  95.     /* Type modifiers.  */
  96.     char is_short, is_long, is_long_double;
  97. #ifdef    HAVE_LONGLONG
  98.     /* We use the `L' modifier for `long long int'.  */
  99. #define    is_longlong    is_long_double
  100. #else
  101. #define    is_longlong    0
  102. #endif
  103. #if FLOATS
  104.     /* Status for reading F-P nums.  */
  105.     char got_dot, got_e;
  106. #endif
  107.     /* If a [...] is a [^...].  */
  108.     char not_in;
  109.     /* Base for integral numbers.  */
  110.     int base;
  111.     /* Integral holding variables.  */
  112.     long int num;
  113.     unsigned long int unum;
  114. #if FLOATS
  115.     /* Floating-point holding variable.  */
  116. # ifdef __M68881__
  117.     long double fp_num;
  118. # else
  119.     double fp_num;
  120. # endif
  121. #endif
  122.     /* Character-buffer pointer.  */
  123.     register char *str;
  124.     /* Workspace.  */
  125.     char work[256];
  126.     char *w;        /* Pointer into WORK.  */
  127.     
  128.     if ((format == NULL) || (!*format))
  129.     {
  130.     errno = EINVAL;
  131.     input_error();
  132.     }
  133.     
  134. # define decimal ('.')    /* should really come from locale stuff that we dont */
  135.     /* have as yet */
  136.     c = inchar();
  137.     
  138.     /* Run through the format string.  */
  139.     while (*f != '\0')
  140.     {
  141. #if 0 /* no mb support as yet */
  142.     if (!isascii(*f))
  143.     {
  144.         /* Non-ASCII, may be a multibyte.  */
  145.         int len = mblen(f, strlen(f));
  146.         if (len > 0)
  147.         {
  148.         while (len-- > 0)
  149.             if (c == EOF)
  150.             input_error();
  151.             else if (c == *f++)
  152.             (void) inchar();
  153.             else
  154.             conv_error();
  155.         continue;
  156.         }
  157.         }
  158. #endif      
  159.     
  160.     fc = *f++;
  161.     if (fc != '%')
  162.     {
  163.         /* Characters other than format specs must just match.  */
  164.         if (c == EOF)
  165.         input_error();
  166.         if (isspace(fc))
  167.         {
  168.         /* Whitespace characters match any amount of whitespace.  */
  169.         while (isspace (c))
  170.             (void)inchar ();
  171.         continue;
  172.         }
  173.         else if (c == fc)
  174.         (void) inchar();
  175.         else
  176.         conv_error();
  177.         continue;
  178.     }
  179.     
  180.     /* Check for the assignment-suppressant.  */
  181.     if (*f == '*')
  182.     {
  183.         do_assign = 0;
  184.         ++f;
  185.     }
  186.     else
  187.         do_assign = 1;
  188.     
  189.     /* Find the maximum field width.  */
  190.     width = 0;
  191.     while (isdigit(*f))
  192.     {
  193.         width *= 10;
  194.         width += *f++ - '0';
  195.     }
  196.     if (width == 0)
  197.         width = -1;
  198.     
  199.     /* Check for type modifiers.  */
  200.     is_short = is_long = is_long_double = 0;
  201.     while (*f == 'h' || *f == 'l' || *f == 'L')
  202.         switch (*f++)
  203.         {
  204.           case 'h':
  205.         /* int's are short int's.  */
  206.         is_short = 1;
  207.         break;
  208.           case 'l':
  209. #ifdef HAVE_LONGLONG
  210.         if (is_long)
  211.             /* A double `l' is equivalent to an `L'.  */
  212.             is_longlong = 1;
  213.         else
  214. #endif
  215.             /* int's are long int's.  */
  216.             is_long = 1;
  217.         break;
  218.           case 'L':
  219.         /* double's are long double's, and int's are long long int's.  */
  220.         is_long_double = 1;
  221.         break;
  222.         }
  223.     
  224.     /* End of the format string?  */
  225.     if (*f == '\0')
  226.         conv_error();
  227.     
  228.     /* Find the conversion specifier.  */
  229.     w = work;
  230.     fc = *f++;
  231.     if (fc != '[' && fc != 'c' && fc != 'n')
  232.         /* Eat whitespace.  */
  233.         while (isspace(c))
  234.         (void) inchar();
  235.     switch (fc)
  236.     {
  237.       case '%':    /* Must match a literal '%'.  */
  238.         if (c != fc)
  239.         conv_error();
  240.         else
  241.         c = inchar();
  242.         break;
  243.         
  244.       case 'n':    /* Answer number of assignments done.  */
  245.         if (do_assign)
  246.         *va_arg(arg, int *) = read_in - 1; /* -1 is debatable ++jrb */
  247.         break;
  248.         
  249.       case 'c':    /* Match characters.  */
  250.         if (do_assign)
  251.         {
  252.         str = va_arg(arg, char *);
  253.         if (str == NULL)
  254.             conv_error();
  255.         }
  256.         
  257.         if (c == EOF)
  258.         input_error();
  259.         
  260.         if (width == -1)
  261.         width = 1;
  262. /* mjr:    */        
  263.         if (do_assign) {
  264.         do
  265.             *str++ = c;
  266.         while (inchar() != EOF && --width > 0);
  267.         } else
  268.         while (inchar() != EOF && --width > 0)
  269.             ;
  270.  
  271.         if (do_assign)
  272.         ++done;
  273.         
  274.         if (c == EOF)
  275.         input_error();
  276.         break;
  277.         
  278.       case 's':    /* Read a string.  */
  279.         if (do_assign)
  280.         {
  281.         str = va_arg(arg, char *);
  282.         if (str == NULL)
  283.             conv_error();
  284.         }
  285.         
  286.         if (c == EOF)
  287.         input_error();
  288.         
  289.         do
  290.         {
  291.         if (isspace(c))
  292.             break;
  293.         if (do_assign)
  294.             *str++ = c;
  295.         } while ((inchar() != EOF) && ((width > 0) ? --width != 0 : 1));
  296.         
  297.         if (do_assign)
  298.         {
  299.         *str = '\0';
  300.         ++done;
  301.         }
  302.         
  303.         if (c == EOF)
  304.         input_error();
  305.         break;
  306.         
  307.       case 'x':    /* Hexadecimal integer.  */
  308.       case 'X':    /* Ditto.  */ 
  309.         base = 16;
  310.         goto number;
  311.         
  312.       case 'o':    /* Octal integer.  */
  313.         base = 8;
  314.         goto number;
  315.         
  316.       case 'u':    /* Decimal integer.  */
  317.       case 'd':    /* Ditto.  */
  318.         base = 10;
  319.         goto number;
  320.         
  321.       case 'i':    /* Generic number.  */
  322.         base = 0;
  323.         
  324.       number:;
  325.         if (c == EOF)
  326.         input_error();
  327.         
  328.         /* Check for a sign.  */
  329.         if (c == '-' || c == '+')
  330.         {
  331.         *w++ = c;
  332.         if (width > 0)
  333.             --width;
  334.         (void) inchar();
  335.         }
  336.         
  337.         /* Look for a leading indication of base.  */
  338.         if (c == '0')
  339.         {
  340.         if (width > 0)
  341.             --width;
  342.         *w++ = '0';
  343.         
  344.         (void) inchar();
  345.         
  346.         if (tolower(c) == 'x')
  347.         {
  348.             /* one char look ahead to see if its really a lead ind */
  349.             int savec = c;
  350.             int peekc = inchar();
  351.             c = savec;
  352.             (void)unchar(peekc);
  353.             if(isxdigit(peekc))
  354.                     {  
  355.             if (base == 0)
  356.                 base = 16;
  357.             if (base == 16)
  358.             {
  359.                 if (width > 0)
  360.                 --width;
  361.                 (void) inchar();
  362.             }
  363.             }
  364.         }
  365.         else if (base == 0)
  366.             if((c >= '0') && (c <= '7')) 
  367.             base = 8;
  368.         }
  369.         
  370.         if (base == 0)
  371.         base = 10;
  372.         
  373.         /* Read the number into WORK.  */
  374.         do
  375.         {
  376.         if (base == 16 ? !isxdigit(c) :
  377.             (!isdigit(c) || ((c - '0') >= base)))
  378.             break;
  379.         *w++ = c;
  380.         if (width > 0)
  381.             --width;
  382.         } while (inchar() != EOF && width != 0);
  383.         
  384.         if (w == work ||
  385.         (w - work == 1 && (work[0] == '+' || work[0] == '-')))
  386.         /* There was no number.  */
  387.         conv_error();
  388.         
  389.         /* Convert the number.  */
  390.         *w = '\0';
  391.         num = strtol(work, &w, base);
  392.         if (w == work)
  393.         conv_error();
  394.         
  395.         if (do_assign)
  396.         {
  397.         if (is_longlong)
  398.             *va_arg(arg, LONGLONG int *) = num;
  399.         else if (is_long)
  400.             *va_arg(arg, long int *) = num;
  401.         else if (is_short)
  402.             *va_arg(arg, short int *) = (short int) num;
  403.         else
  404.             *va_arg(arg, int *) = (int) num;
  405.         ++done;
  406.         }
  407.         break;
  408.         
  409. #if FLOATS
  410.       case 'e':    /* Floating-point numbers.  */
  411.       case 'E':
  412.       case 'f':
  413.       case 'g':
  414.       case 'G':
  415.         if (c == EOF)
  416.         input_error();
  417.         
  418.         /* Check for a sign.  */
  419.         if (c == '-' || c == '+')
  420.         {
  421.         *w++ = c;
  422.         if (inchar() == EOF)
  423.             input_error();
  424.         if (width > 0)
  425.             --width;
  426.         }
  427.         
  428.         got_dot = got_e = 0;
  429.         do
  430.         {
  431.         if (isdigit(c))
  432.             *w++ = c;
  433.         else if (got_e && w[-1] == 'e' && (c == '-' || c == '+'))
  434.             *w++ = c;
  435.         else if (!got_e && tolower(c) == 'e')
  436.         {
  437.             *w++ = 'e';
  438.             got_e = got_dot = 1;
  439.         }
  440.         else if (c == decimal && !got_dot)
  441.         {
  442.             *w++ = c;
  443.             got_dot = 1;
  444.         }
  445.         else
  446.             break;
  447.         if (width > 0)
  448.             --width;
  449.         } while (inchar() != EOF && width != 0);
  450.         
  451.         if (w == work)
  452.         conv_error();
  453. #if 0
  454.         if (w[-1] == '-' || w[-1] == '+' || w[-1] == 'e')
  455.         conv_error();
  456. #else
  457.         if (w[-1] == '-' || w[-1] == '+')
  458.         conv_error();
  459.  
  460.         if(tolower(w[-1]) == 'e')
  461.         {
  462.         unchar(c);
  463.             --w;
  464.             c = *w;
  465.         }
  466. #endif        
  467.         /* Convert the number.  */
  468.         *w = '\0';
  469.         fp_num = strtod(work, &w);
  470.         if (w == work)
  471.         conv_error();
  472.         
  473.         if (do_assign)
  474.         {
  475.         if (is_long_double)
  476. #ifdef __M68881__
  477.             *va_arg(arg, long double *) = fp_num;
  478. #else
  479.             *va_arg(arg, double *) = fp_num;
  480. #endif
  481.         else if (is_long)
  482.             *va_arg(arg, double *) = (double) fp_num;
  483.         else
  484.             *va_arg(arg, float *) = (float) fp_num;
  485.         ++done;
  486.         }
  487.         break;
  488. #endif /* FLOATS */
  489.         
  490.       case '[':    /* Character class.  */
  491.         if (do_assign)
  492.         {
  493.         str = va_arg(arg, char *);
  494.         if (str == NULL)
  495.             conv_error();
  496.         }
  497.         
  498.         if (c == EOF)
  499.         input_error();
  500.         
  501.         if (*f == '^')
  502.         {
  503.         ++f;
  504.         not_in = 1;
  505.         }
  506.         else
  507.         not_in = 0;
  508.         w = (char *)f;        /* remember start of class */
  509.         bzero (work, 256);
  510.         while ((fc = *f++) != '\0' && (fc != ']' || f - 1 == w))
  511.         {
  512.         /* Add the character to the list.  */
  513.         work[(unsigned char)fc] = 1;
  514.         /* Look ahead for a range.  */
  515.         if (f[0] == '-' && f[1] != '\0' && f[1] != ']')
  516.           {
  517.             /* Add all characters from the one before the '-'
  518.                up to the next format char.  */
  519.             unsigned char end = f[1];
  520.             while ((unsigned char)++fc <= end)
  521.             work[(unsigned char)fc] = 1;
  522.             f += 2;
  523.           }
  524.         }
  525.         if (fc == '\0')
  526.         conv_error();
  527.         
  528.         work[0] = not_in;
  529.         unum = read_in;
  530.         do
  531.         {
  532.         if (work[(unsigned char)c] == not_in)
  533.             break;
  534.         if (do_assign)
  535.             *str++ = c;
  536.         if (width > 0)
  537.             --width;
  538.         } while (inchar() != EOF && width != 0);
  539.         if (read_in == unum)
  540.         conv_error();
  541.         
  542.         if (do_assign)
  543.         {
  544.         *str = '\0';
  545.         ++done;
  546.         }
  547.         break;
  548.         
  549.       case 'p':    /* Generic pointer.  */
  550.         base = 16;
  551.         /* A PTR must be the same size as a `long int'.  */
  552.         is_long = 1;
  553.         goto number;
  554.     }
  555.     }
  556.     
  557.     conv_error();
  558. }
  559.